#!/bin/sh
################################################################################
# NAME
# @(#) logger.sh
# 
# COPYRIGHT
#   (c) Warren Chisholm. All rights reserved.
#
# DESCRIPTION
#   Common logging functions.
#
# AUTHOR
#   Warren Chisholm
#
# EXAMPLE
#   include at the top of a script
#   . ./logger.sh
#
# MODIFICATION HISTORY
#   2007-11-22	wchisholm		Initial Version.
#   2012-01-30	wchisholm		Changes for QNAP.
#
################################################################################

logger_ALL_LEVEL=0
logger_TRACE_LEVEL=1
logger_DEBUG_LEVEL=2
logger_INFO_LEVEL=3
logger_WARN_LEVEL=4
logger_ERROR_LEVEL=5
logger_FATAL_LEVEL=6
logger_OFF_LEVEL=99

logger_TRACE_LEVEL_TEXT="T"
logger_DEBUG_LEVEL_TEXT="D"
logger_INFO_LEVEL_TEXT="I"
logger_WARN_LEVEL_TEXT="W"
logger_ERROR_LEVEL_TEXT="E"
logger_FATAL_LEVEL_TEXT="F"

logger_TRACE_LEVEL_TEXT_LONG="TRACE"
logger_DEBUG_LEVEL_TEXT_LONG="DEBUG"
logger_INFO_LEVEL_TEXT_LONG="INFO"
logger_WARN_LEVEL_TEXT_LONG="WARN"
logger_ERROR_LEVEL_TEXT_LONG="ERROR"
logger_FATAL_LEVEL_TEXT_LONG="FATAL"

#only log statements with level equal to or higher than threshold.
logger_logthreshold=${logger_ALL_LEVEL}

#trap any script exits
trap 'logExitScript ${scriptname} ${LINENO}' EXIT

function errtrap
{
	typeset es=${?}
	typeset scriptname=${1}
	typeset lineno=${2}
	logError "Command exited with status $es." ${scriptname} ${lineno}
}
trap 'errtrap $scriptname $LINENO' ERR

#
# log level message [scriptname [lineno]]
#
function log
{
	typeset level=${1}
	typeset message=${2}
	typeset scriptname=${3:-?}
	typeset lineno=${4:-?}

	if [ ${level} -ge ${logger_logthreshold} ]
	then
		typeset leveltext="?"
		case ${level} in
			${logger_TRACE_LEVEL}) leveltext=${logger_TRACE_LEVEL_TEXT};;
			${logger_DEBUG_LEVEL}) leveltext=${logger_DEBUG_LEVEL_TEXT};;
			${logger_INFO_LEVEL} ) leveltext=${logger_INFO_LEVEL_TEXT};;
			${logger_WARN_LEVEL} ) leveltext=${logger_WARN_LEVEL_TEXT};;
			${logger_ERROR_LEVEL}) leveltext=${logger_ERROR_LEVEL_TEXT};;
			${logger_FATAL_LEVEL}) leveltext=${logger_FATAL_LEVEL_TEXT};;
			*);;
		esac
		typeset userlogin=`id -un`
		printf "%s %-1s [%-8s|%6s] %10s - %3s:%s\n" "`date +'%Y-%m-%d %H:%M:%S'`" "${leveltext}" "${userlogin}" "${$}" "${scriptname}" "${lineno}" "${message}"
	fi
}

#
# logEnterScript [scriptname [lineno]]
#
function logEnterScript
{
	typeset scriptname=${1}
	typeset lineno=${2}
	log ${logger_TRACE_LEVEL} "Entered Script." ${scriptname} ${lineno}
}

#
# logExitScript [scriptname [lineno]]
#
function logExitScript
{
	typeset scriptname=${1}
	typeset lineno=${2}
	log ${logger_TRACE_LEVEL} "Exited  Script." ${scriptname} ${lineno}
}

#
# logEnterFunction functionname [scriptname [lineno]]
#
function logEnterFunction
{
	typeset functionname=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_TRACE_LEVEL} "Entered Function ${functionname}." ${scriptname} ${lineno}
}

#
# logExitFunction functionname [scriptname [lineno]]
#
function logExitFunction
{
	typeset functionname=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_TRACE_LEVEL} "Exited  Function ${functionname}." ${scriptname} ${lineno}
}

#
# logDebug message [scriptname [lineno]]
#
function logDebug
{
	typeset message=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_DEBUG_LEVEL} "${message}" ${scriptname} ${lineno}
}

#
# logInfo message [scriptname [lineno]]
#
function logInfo
{
	typeset message=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_INFO_LEVEL}  "${message}" ${scriptname} ${lineno}
}

#
# logWarn message [scriptname [lineno]]
#
function logWarn
{
	typeset message=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_WARN_LEVEL} "${message}" ${scriptname} ${lineno}
}

#
# logError message [scriptname [lineno]]
#
function logError
{
	typeset message=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_ERROR_LEVEL} "${message}" ${scriptname} ${lineno}
}

#
# logFatal message [scriptname [lineno]]
#
function logFatal
{
	typeset message=${1}
	typeset scriptname=${2}
	typeset lineno=${3}
	log ${logger_FATAL_LEVEL} "${message}" ${scriptname} ${lineno}
}